package com.terrorism.sdk.utils;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Display;
import android.view.WindowManager;

/**
 * @since Monday, June 02, 2014
 * @author hoangcaomobile
 *
 */
public class BitmapUtils {

	/** 
	 * photos
	 */
	// get bitmap smart
	public static Bitmap getBitmap(Activity activity, Bitmap bitmap, int centOfWidth) {
		Bitmap newBitmap = ThumbnailUtils.extractThumbnail(bitmap,
				(getScreenWidth(activity) / 100) * centOfWidth,
				(getScreenWidth(activity) / 100) * centOfWidth);
		return newBitmap;
	}
	// --

	// get bitmap match with screen
	public static Bitmap getBitmapMatchScreen(Bitmap bitmap, Activity activity) {
		Bitmap newBitmap = ThumbnailUtils.extractThumbnail(bitmap,
				(getScreenWidth(activity) / 4) * 3,
				(getScreenWidth(activity) / 4) * 3);
		return newBitmap;
	}
	// --

	// get bitmap match with screen
	public static Bitmap getBitmapMessage(Bitmap bitmap, Activity activity) {
		Bitmap newBitmap = ThumbnailUtils.extractThumbnail(bitmap,
				(getScreenWidth(activity) / 4),
				(getScreenWidth(activity) / 4));
		return newBitmap;
	}
	// --

	// get bitmap match for user wall
	public static Bitmap getBitmapUserCover(Bitmap bitmap, Activity activity) {
		Bitmap newBitmap = ThumbnailUtils.extractThumbnail(bitmap,
				getScreenWidth(activity),
				getScreenWidth(activity) - (getScreenWidth(activity) / 4));
		//		Bitmap newBitmap = ThumbnailUtils.extractThumbnail(bitmap, getScreenWidth(activity), getScreenWidth(activity) - (getScreenWidth(activity) / 4));
		return newBitmap;
	}
	// --

	// getBitmapAvatarSquare
	public static Bitmap getBitmapAvatarSquare(Bitmap bitmap, Activity activity) {
		Bitmap squareBitmap = ThumbnailUtils.extractThumbnail(bitmap,
				getScreenWidth(activity) / 9,
				getScreenWidth(activity) / 9);
		//		Bitmap squareBitmap = ThumbnailUtils.extractThumbnail(bitmap, getScreenWidth(activity) / 8, getScreenWidth(activity) / 8);
		return squareBitmap;
	}
	// --

	// convert bitmap circle
	public static  Bitmap getBitmapAvatarCircle(Bitmap bitmapOrigin, Activity activity) {
		Bitmap bitmap = ThumbnailUtils.extractThumbnail(bitmapOrigin,
				(getScreenWidth(activity) / 10),
				(getScreenWidth(activity) / 10));
		Bitmap output;

		if (bitmap.getWidth() > bitmap.getHeight()) {
			output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
		} else {
			output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Config.ARGB_8888);
		}

		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

		float r = 0;

		if (bitmap.getWidth() > bitmap.getHeight()) {
			r = bitmap.getHeight() / 2;
		} else {
			r = bitmap.getWidth() / 2;
		}

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawCircle(r, r, r, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}
	// --

	// convert bitmap circle
	public static  Bitmap getBitmapAvatarCircleProfile(Bitmap bitmapOrigin, Activity activity) {
		Bitmap bitmap = ThumbnailUtils.extractThumbnail(bitmapOrigin,
				(getScreenWidth(activity) / 6),
				(getScreenWidth(activity) / 6));
		Bitmap output;

		if (bitmap.getWidth() > bitmap.getHeight()) {
			output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
		} else {
			output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Config.ARGB_8888);
		}

		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

		float r = 0;

		if (bitmap.getWidth() > bitmap.getHeight()) {
			r = bitmap.getHeight() / 2;
		} else {
			r = bitmap.getWidth() / 2;
		}

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawCircle(r, r, r, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}
	// --

	// get size of screen width
	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	public static int getScreenWidth(Activity activity) {
		int screenWidth = 0;  
		//		int screenHeight = 0;  
		Point size = new Point();
		WindowManager w = activity.getWindowManager();

		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)    {
			w.getDefaultDisplay().getSize(size);
			screenWidth = size.x;
			//			screenHeight = size.y; 
		} else {
			Display d = w.getDefaultDisplay(); 
			screenWidth = d.getWidth(); 
			//			screenHeight = d.getHeight(); 
		}
		return screenWidth;

	}
	// --

	// check camera access
	public static boolean isHaveCamera(Activity activity) {
		PackageManager pm = activity.getPackageManager();
		if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
			return true;
		} else {
			return false;
		}
	}
	// --

	/**
	 * memory
	 * @param folderName
	 * @return
	 */
	// check ensure SD Card Access and create a folder
	public static boolean isSDCardOK(String folderName) {
		File file = new File(Environment.getExternalStorageDirectory() + "/" + folderName);
		if (file.exists()) {
			return true;
		} else if (file.mkdirs()) {
			return true;
		}
		return false;
	}
	// --

	// get photo path from uri
	public static final String getPhotoPathFromUri(Uri uri, Activity activity) {
		String[] projection = { MediaStore.Images.Media.DATA };
		@SuppressWarnings("deprecation")
		Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
		int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}
	// --

	// get file name from source file
	public static final String getFileName(String sourceFile) {
		File objFile = new File(sourceFile);
		String fileName = objFile.getName();
		return fileName;
	}
	// --

}
